/**
 *	@file	telseq_grandpa.c
 *	@brief	育て屋じいさん　会話データ抽選＆再生
 *	@author	Miyuki Iwasawa
 *	@date	09.02.06
 */

#include "common.h"
#include "system/procsys.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/fontproc.h"
#include "system/snd_tool.h"
#include "system/buflen.h"
#include "system/pm_rtc.h"
#include "gflib/touchpanel.h"
#include "field/eventflag.h"
#include "field/evwkdef.h"
#include "field/sysflag.h"
#include "fielddata/script/saveflag.h"
#include "itemtool/itemsym.h"
#include "savedata/sodateyadata.h"
#include "field/sodateya.h"
#include "msgdata/msg.naix"
#include "msgdata/msg_pg_tel.h"
#include "msgdata/msg_tel_p_grandpa.h"

#include "../pm_tel.h"
#include "tel/pmtel_def.h"
#include "tel/pmtel_datman.h"
#include "tel/pmtel_dat_def.h"
#include "../pmtel_seq.h"
#include "telseq_play.h"

/**
 *	@brief	電話　育て屋じいさんの会話抽選
 */
u16 PMTelPlayL_Grandpa(PMTEL_DATA_MAN* man,PMTEL_TALK_WORK* wk)
{
	int i;
	SODATEYA_WORK* sp;
	wk->seq_code = TEL_SEQ_SIMPLE;

	//ノーマル着信
	if(wk->is_receive){
		if(EventWork_CheckEventFlag(man->ev_sv,SYS_FLAG_SODATEYA_EGG_GET_1ST)){
			return TEL_TK_GRANDPA_EGG_GET_2ND;
		}
		return TEL_TK_GRANDPA_EGG_GET_1ST;
	}

#if 0	//じいちゃんは同一マップでも通常会話になる 090529 iwasawa
	if(wk->pbk->map_id == man->zone_id)	{
		return TEL_TK_GRANDPA_NE;
	}
#endif

	sp = SaveData_GetSodateyaWork(man->save);
	wk->tmp_wk = (void*)sp;

	wk->tmp_u8 = 0;
	for(i = 0;i < SODATEYA_POKE_MAX;i++){
		SODATEYA_POKE* pp = SodateyaWork_GetSodateyaPokePointer( sp, i);

		if(PokePasoParaGet(SodateyaPoke_GetPokePasoPointer(pp), ID_PARA_monsno, NULL)){
			WORDSET_RegisterPokeNickName( man->wset,
				WORDSET_TEL_LOCAL_BUF+i,
				SodateyaPoke_GetPokePasoPointer(pp));	//ポケモンの名前をセット
			wk->tmp_u8++;
		}
	}
	if(wk->tmp_u8 == 2){
		//二体の相性を取得
		wk->tmp_u16 = SodateyaGetAffinity(sp);
	}
	wk->seq_code = TEL_SEQ_GRANDPA;
	return 0;
}

/**
 *	@brief	育てじいさんの会話シーケンス
 */
int PMTelSeq_TypeGrandpa(PMTEL_DATA_MAN* man)
{
	PMTEL_TALK_WORK *wk = &man->twk;
	switch(wk->seq){
	case 0:
		//タグ展開処理
		PMTel_TagSpreadCommon(man);

		//挨拶出力
		PMTel_MsgPrintGmm(man,man->m_person,m_grandpa_10);
		break;
	case 1:
		if(!PMTel_MsgPrintWait(man)) return FALSE;

		if(SodateyaWork_GetEggFlag((const SODATEYA_WORK*)wk->tmp_wk)){
			//タマゴが生まれている
			PMTel_MsgPrintGmm(man,man->m_person,m_grandpa_11);
			wk->seq = 0xFF;
		}else if(wk->tmp_u8 == 0){
			//預かってるポケモンはいない
			PMTel_MsgPrintGmm(man,man->m_person,m_grandpa_12);
			wk->seq = 3;
		}else if(wk->tmp_u8 == 1){
			wk->seq= 3;
			PMTel_MsgPrintGmm(man,man->m_person,m_grandpa_13);
		}else{
			wk->seq = 2;
			PMTel_MsgPrintGmm(man,man->m_person,m_grandpa_14);
		}
		return FALSE;
	case 2:
		if(!PMTel_MsgPrintWait(man)) return FALSE;

		//相性
		PMTel_MsgPrintGmm(man,man->m_person,m_grandpa_15+ wk->tmp_u16);
		break;
	case 3:
		if(!PMTel_MsgPrintWait(man)) return FALSE;
		//いつでも顔を見せなさい
		PMTel_MsgPrintGmm(man,man->m_person,m_grandpa_19);
		break;
	default:
		if(!PMTel_MsgPrintWait(man)) return FALSE;
		MSGMAN_Delete(man->m_person);
		return TRUE;
	}
	++wk->seq;
	return FALSE;
}
